package com.qsmx.core.util.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import com.qsmx.core.R;
import com.qsmx.core.delegates.AtmosDelegate;
import com.qsmx.core.util.ClickUtil;
import com.qsmx.core.util.CountDownTimerUtil;
import com.qsmx.core.util.dimen.DimenUtil;


/**
 * 文 件 名: DialogUtil.java
 * 描 述: 对话框工具类
 * 创 建 人: dingyu
 * 创建时间: 2015-10-23
 */
@SuppressLint({"NewApi", "InflateParams"})
public class DialogUtil {

    private static CountDownTimerUtil timer;
    private static Dialog mTimeDialog;
    private static AppCompatTextView tvBackHome;
    private static AtmosDelegate DELEGATE;
    private static Dialog errorDialog;
    private static Dialog serviceSureDialog;
    private static Dialog loadingDialog;
    private static AppCompatTextView tvErrorInfo;
    private static AppCompatTextView tvRetry;

    /**
     * 显示自定义提示信息加载对话框
     */
    public static Dialog createLoadingDialog(Context context, String message, boolean isCancel) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_loading, null);

        TextView tv = (TextView) view.findViewById(R.id.loading_tv);
        AppCompatImageView ivLoading = view.findViewById(R.id.iv_loading);

        Animation rotate = AnimationUtils.loadAnimation(context, R.anim.rotate_anim);
        if (rotate != null) {
            ivLoading.startAnimation(rotate);
        } else {
            ivLoading.setAnimation(rotate);
            ivLoading.startAnimation(rotate);
        }

        tv.setText(message);
        Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(view);
        dialog.setCancelable(isCancel);
        return dialog;
    }

    /**
     * 显示对话框
     */
    public static void showDialog(Dialog dialog) {
        if (null != dialog && !dialog.isShowing()) {
            dialog.show();
        }
    }

    /**
     * 关闭对话框
     */
    public static void closeDialog(Dialog dialog) {
        if (null != dialog && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public static void showCardErrorDialog(AtmosDelegate mDelegate, Context mContext, String errInfo, String bottomText, final View.OnClickListener listener) {
        try {
            if (mDelegate.isAdded()) {
                errorDialog = new Dialog(mContext, R.style.dialog);
                errorDialog.setCancelable(false);
                Window window = errorDialog.getWindow();
                window.setContentView(R.layout.dialog_error_tips);
                int width = (int) (DimenUtil.getScreenWidth() * 0.8);
                window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

                tvErrorInfo = window.findViewById(R.id.tv_error_info);
                tvRetry = window.findViewById(R.id.tv_retry);

                tvErrorInfo.setText(errInfo);
                tvRetry.setText(bottomText);
                tvRetry.setOnClickListener(listener);
                errorDialog.show();
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    public static void showCardErrorDialog(AtmosDelegate mDelegate, Context mContext, String
            errInfo, String bottomText) {
        try {
            if (mDelegate.isAdded()) {
                errorDialog = new Dialog(mContext, R.style.dialog);
                Window window = errorDialog.getWindow();
                window.setContentView(R.layout.dialog_error_tips);
                int width = (int) (DimenUtil.getScreenWidth() * 0.8);
                window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

                AppCompatTextView tvErrorInfo = window.findViewById(R.id.tv_error_info);
                AppCompatTextView tvRetry = window.findViewById(R.id.tv_retry);

                tvErrorInfo.setText(errInfo);
                tvRetry.setText(bottomText);
                tvRetry.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (ClickUtil.isNotFastClick()) {
                            errorDialog.dismiss();
                        }
                    }
                });
                errorDialog.show();
            }
        } catch (Exception e) {

        }
    }

    public static void showCardErrorDialog(Context mContext, String errInfo, String bottomText) {
        try {
            errorDialog = new Dialog(mContext, R.style.dialog);
            Window window = errorDialog.getWindow();
            window.setContentView(R.layout.dialog_error_tips);
            int width = (int) (DimenUtil.getScreenWidth() * 0.8);
            window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

            AppCompatTextView tvErrorInfo = window.findViewById(R.id.tv_error_info);
            AppCompatTextView tvRetry = window.findViewById(R.id.tv_retry);

            tvErrorInfo.setText(errInfo);
            tvRetry.setText(bottomText);
            tvRetry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ClickUtil.isNotFastClick()) {
                        errorDialog.dismiss();
                    }
                }
            });
            errorDialog.show();
        } catch (Exception e) {

        }
    }

    public static void showLoadingDialog(final Context mContext, String message) {
        loadingDialog = new Dialog(mContext, R.style.dialog);
        loadingDialog.setCancelable(false);
        Window window = loadingDialog.getWindow();
        window.setContentView(R.layout.dialog_loading);
        int width = (int) (DimenUtil.getScreenWidth() * 0.8);
        window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

        TextView tv = window.findViewById(R.id.loading_tv);
        AppCompatImageView ivLoading = window.findViewById(R.id.iv_loading);

        Animation rotate = AnimationUtils.loadAnimation(mContext, R.anim.rotate_anim);
        if (rotate != null) {
            ivLoading.startAnimation(rotate);
        } else {
            ivLoading.setAnimation(rotate);
            ivLoading.startAnimation(rotate);
        }

        tv.setText(message);

        loadingDialog.show();
    }

    public static void showHasNoPermissionDialog(final Context mContext, final View.OnClickListener retryListener, final View.OnClickListener closeListener) {
        Dialog dialog = new Dialog(mContext, R.style.dialog);
        dialog.setCancelable(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_has_no_permission);
        int width = (int) (DimenUtil.getScreenWidth() * 0.9);
        window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

        AppCompatTextView tvRetry = window.findViewById(R.id.tv_retry);
        AppCompatTextView tvClose = window.findViewById(R.id.tv_close);

        tvRetry.setOnClickListener(retryListener);

        tvClose.setOnClickListener(closeListener);

    }

    public static void dismissLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    public static void dismissErrorDialog() {
        if (errorDialog != null) {
            errorDialog.dismiss();
        }
    }

    private static String getHtmlData(String bodyHTML) {
        String head = "<head>"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:100%; height:auto;}*{margin:0px;}</style>"
                + "</head>";
        return "<html>" + head + "<body style=\"background-color: #00000000;\">" + bodyHTML + "</body></html>";
    }

    public static void dismissDoSureDialog() {
        if (serviceSureDialog != null) {
            serviceSureDialog.dismiss();
        }
    }
}
